<?php

namespace App\Http\Controllers\Login;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{

    //登录
    public function login(Request $request)
    {
        //登录页面
        if($request->method()=="GET"){
            return view('login.login');
        }
        $param = $request->all();
        //定义验证规则
        $rules = [
            'email' => 'required|email',
            'password' => 'required|min:6|max:20'
        ];
        //定义报错信息
        $messages = [
            'email.required' => '登录邮箱不可为空',
            'email.email' => '邮箱格式不正确',
            'password.min' => '用户名长度最少6位',
            'password.max' => '用户名长度最大不能超过20',
            'password.required' => '密码不能为空'
        ];
        //将$res里面传过来的数据进行判断
        $validator =  Validator::make($param,$rules,$messages);
        //如果判断为真  就会跳到控制器error
        if ($validator->fails()){
            if(isset($validator->getMessageBag()->getMessages()['email'])){
                $error = $validator->getMessageBag()->getMessages()['email'][0];
            }else{
                $error = $validator->getMessageBag()->getMessages()['password'][0];
            }
            return view('login.login',[
                'message'=>$error,
            ]);
        }
        $user = DB::table('users')->where([
            'username'=>$param['email']
        ])->first();
        if(!$user){
            return view('login.login',[
                'message'=>"此邮箱还未注册",
            ]);
        }
        if(!Hash::check($param['password'],$user->password)){
            return view('login.login',[
                'message'=>"账号或密码错误",
            ]);
        }
        $user->user_id = $user->id;
        session(['user'=>$user]);
        //登录成功逻辑
        return redirect('/');

    }

    //注册
    public function register(Request $request)
    {

        if($request->method()=="GET"){
            return view('login.register');
        }
        //注册处理
        $param = $request->all();
        //定义验证规则
        $rules = [
            'nickname'=>'required',
            'email' => 'required|email',
            'password' => 'required|min:6|max:20'
        ];
        //定义报错信息
        $messages = [
            'nickname.required' => '昵称不可为空',
            'email.required' => '邮箱不可为空',
            'email.email' => '邮箱格式不正确',
            'password.required' => '密码不能为空'
        ];
        //将$res里面传过来的数据进行判断
        $validator =  Validator::make($param,$rules,$messages);
        //如果判断为真  就会跳到控制器error
        if ($validator->fails()){
            if(isset($validator->getMessageBag()->getMessages()['email'])){
                $error = $validator->getMessageBag()->getMessages()['email'][0];
            }else{
                $error = $validator->getMessageBag()->getMessages()['password'][0];
            }
            return view('login.register',[
                'message'=>$error,
            ]);
        }
        $user = DB::table('users')->where([
            'username'=>$param['email']
        ])->first();
       if($user){
           return view('login.register',[
               'message'=>"此邮箱已注册",
           ]);
       }
        $nickname = $param['nickname'];
        $email = $param['email'];
        $password = Hash::make($param['password']);

        $result = DB::table('users')->insert([
            'username'=>$email,
            'nickname'=>$nickname,
            'email'=>$email,
            'password'=>$password,
            'created_at'=>date('Y-m-d H:i:s')
        ]);
        if(!$result){
            return view('login.register',[
                'message'=>"系统繁忙，请稍后再试！",
            ]);
        }

        return view('login.login',[
            'message'=>"注册成功，请登录！",
        ]);


    }


    //退出登录
    public function logout()
    {
        session()->remove('user');

        return redirect('/');
    }

}
